package com.example.juc.big_talk_concurrent.chapter6.article2;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class Test {
    // 同一时刻参与竞争的人数 10
    static ExecutorService executorService = Executors.newFixedThreadPool(10);
    // 椅子数量 3
    static Semaphore semaphore = new Semaphore(3);

    public static void main(String[] args) {

        // 总人数 30
        for (int i = 0; i < 30; i++) {
            int finalI = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(finalI);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    semaphore.release();
                }
            });
        }

        executorService.shutdown();
    }
}
